package simulator.failure;

import java.util.HashMap;
import java.util.Random;
import simulator.*;

public class WeibullGenerator implements EventGenerator{
	private static Random rand = new Random();
	
	private double gamma;
	private double lamda;
	private double beta;
	private double startTime;
	private String name;

	
	public void reset(double currentTime){
		this.startTime = currentTime;
	}
	
	private double F(double currentTime){
		//System.out.println(currentTime+" "+(1-Math.exp(-Math.pow((currentTime/lamda), beta))));
		return 1-Math.exp(-Math.pow((currentTime/lamda), beta));
	}
	
	public double generateNextEvent(double currentTime) {
		currentTime-=startTime;
		double r = rand.nextDouble();
		double R=(1-F(currentTime))*r+F(currentTime);
		double result = lamda*(Math.pow(-Math.log(1.0-R), 1/beta))+gamma+startTime;
		//if(new Double(result).isInfinite()){
		//	System.out.println("currentTime="+currentTime+" r="+r+" R="+R+" F="+F(currentTime));
		//}
		return lamda*(Math.pow(-Math.log(1.0-R), 1/beta))+gamma+startTime;
	}

	public void init(String name, HashMap<String, String> parameters) {
		this.gamma = Double.parseDouble(parameters.get("gamma"));
		this.lamda = Double.parseDouble(parameters.get("lamda"));;
		this.beta = Double.parseDouble(parameters.get("beta"));;
		this.name = name;
	}

	public String getName(){
		return this.name;
	}
}
